// pages/regist_lib/regist_lib.js
const baseURL = 'http://100.2.178.56:8080'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isSelect1: false,
        isSelect2: false,
        stuId: '',
        stuName: '',
        stuPass: '',
        stuPhone: '',
        academyList: '',
        gradeList: '',
        grade: '',
        academy: '',
        openId: ''
    },
    //点击控制下拉框的展示、隐藏
    select1: function () {
        var isSelect = this.data.isSelect1
        this.setData({
            isSelect1: !isSelect
        })
    },
    select2: function () {
        var isSelect = this.data.isSelect2
        this.setData({
            isSelect2: !isSelect
        })
    },
    //点击下拉框选项，选中并隐藏下拉框
    getAcademy: function (e) {

        let value = e.currentTarget.dataset.type;
        var that = this;
        this.setData({
                academy: value,
                isSelect1: false,
            }),
            // 通过学院名称获取对应的班级
            wx.request({
                url: baseURL+'/student/regist/grades',
                method: "GET",
                data: {
                    academy: value
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        that.setData({
                            gradeList: resData.data.gradeListAca,
                        })
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            })


    },
    //点击下拉框选项，选中并隐藏下拉框
    getGrade: function (e) {
        let value = e.currentTarget.dataset.type
        this.setData({
            grade: value,
            isSelect2: false,
        })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getOpenid()
        var that = this;
        wx.request({
            url: baseURL+'/student/regist_index',
            method: "GET",
            success: function (res) {
                var resData = res.data;
                if (resData.code == 0) {
                    that.setData({
                        gradeList: resData.data.gradeList,
                        academyList: resData.data.academyList
                    })
                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }
            }
        })

    },
    //表单的值传递回来
    input_stuId: function (e) {
        this.setData({
            stuId: e.detail.value,
        })
    },
    input_stuPass: function (e) {
        this.setData({
            stuPass: e.detail.value,
        })
    },
    input_stuPhone: function (e) {
        this.setData({
            stuPhone: e.detail.value,
        })
    },
    input_stuName: function (e) {
        this.setData({
            stuName: e.detail.value,
        })
    },
    /**
     * 注册
     */
    update_information_click: function () {

        /**
         * 先检查内容有没有发生改变，有改变才调用
         */

        var checkstate = true;
        if (this.data.stuName === getApp().globalUserInfo.stuName) {
            if (this.data.stuPhone === getApp().globalUserInfo.stuPhone) {
                if (this.data.grade === getApp().globalGrade.graName) {
                    checkstate = false
                }
            }
        }


        //后台验证
        var that = this;
        var token = getApp().token;
        if (checkstate) {
            wx.request({
                url: baseURL+'/student/update_information_apply',
                method: "POST",
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Authorization': token

                },
                data: {
                    // 因为在onload就已经给stuId和stuPhone赋值，所以变量类型已经改成整形了，所以无法使用trim（）
                    'stuId': that.data.stuId,
                    'stuPass': that.data.stuPass.trim(),
                    'stuName': that.data.stuName.trim(),
                    'academy': this.data.academy.trim(),
                    'grade': this.data.grade.trim(),
                    'stuPhone': this.data.stuPhone,
                    'openId': this.data.openId
                    // 'token':
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        wx.showToast({
                                title: '提交成功',
                                duration: 2000
                            }),
                            setTimeout(function () {
                                wx.switchTab({
                                    url: '/pages/main_lib/main_lib',
                                })
                              }, 1500)
                            
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '没有改',
            })
        }

    },
    /**
     * 获取当前微信用户openId
     */
    getOpenid() {
        let that = this;
        wx.cloud.callFunction({
            config: {
                env: 'librarydfl-6gqf4v6lfb50a164'
            },
            name: 'getOpenId',
            complete: res => {
                console.log('云函数获取到的openid: ', res.result.userInfo)
                var openid = res.result.userInfo.openId;
                that.data.openId = openid
                that.setData({
                    openId: openid
                })
                // 这里可以添加一些函数利用openid实现一些功能
                //this.judgeRusults(openid);
            }
        })
    },

    /**
     * 表单验证：
     * 1、学号：不能为空
     * 2、姓名：不能为空
     * 3、电话：不能为空/请输入正确的电话号码
     * 学院、班级在点击按钮时提醒
     * @param {*} e 
     */
    validate(e) {
        if (e.currentTarget.dataset.name == 'stuId') {
            if (e.detail.value == '') {
                wx.showToast({
                    title: "学号不能为空",
                    duration: 2000
                })
            }
        }
        if (e.currentTarget.dataset.name == 'stuName') {
            if (e.detail.value == '') {
                wx.showToast({
                    title: "姓名不能为空",
                    duration: 2000
                })
            }
        }
        if (e.currentTarget.dataset.name == 'stuPhone') {
            if (e.detail.value == '') {
                wx.showToast({
                    title: "电话不能为空",
                    duration: 2000
                })
            }
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            stuId: getApp().globalUserInfo.stuId,
            stuName: getApp().globalUserInfo.stuName,
            stuPhone: getApp().globalUserInfo.stuPhone,
            academy: getApp().globalAcademy.acaName,
            grade: getApp().globalGrade.graName
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})